home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / cmln1084.arc / CPORT1.LTG < prev    next >
Text File  |  1986-10-01  |  2KB  |  112 lines

  1. /* dgt2.c - test the dgsm.c  and dgsx.c routines */
  2.  
  3. #include <stdio.h>
  4. #include "dgsdefs.h"
  5.  
  6. #define    NULL    0
  7. #define    MAXLEN    50
  8. #define    DEPTH    5
  9.  
  10. main()
  11. {
  12. FILE *fopen(),*fp;
  13. struct tblctrl *tctrl;
  14. struct xtbctrl *xctrl;
  15. char fname[30],str[MAXLEN],*malloc();
  16. int nsym;
  17.  
  18. for(;;)
  19. {
  20. fprintf(stdout,"Enter file name:  ");
  21. if(fscanf(stdin,"%s",fname) != 1)
  22. fprintf(stderr,"Syntax error on file name entry!");
  23. else if((fp=fopen(fname,"r")) != NULL)
  24. break;
  25. else
  26. fprintf(stderr,"Invalid file name!\n");
  27. }
  28. /* Initialize the control table    */
  29. tctrl=galloc();
  30. intdgs(tctrl,DEPTH);
  31. /* Read the tokens into the dgs    */
  32. nsym=0;
  33. while(gtstr(fp,str,MAXLEN) != EOF)
  34. {
  35. fprintf(stdout,"%s\n",str);
  36. nsym++;
  37. if(dgs(tctrl,str,nsym) != nsym)
  38. {
  39. fprintf(stderr,"Error on # %3d, %s\n",nsym,str);
  40. exit(1);
  41. }
  42. }
  43. /* "Rewind" the input file    */
  44. fclose(fp);
  45. if((fp=fopen(fname,"r")) == NULL)
  46. {
  47. fprintf(stderr,"Unable to reopen input file!\n");
  48. exit(1);
  49. }
  50. nsym=0;
  51. while(gtstr(fp,str,MAXLEN) != EOF)
  52. {
  53. nsym++;
  54. if(srdgs(tctrl,str) != nsym)
  55. fprintf(stdout,"Symbol #%3d not found!\n",nsym);
  56. }
  57. fclose(fp);
  58. fprintf(stdout,"# entered: %3d\n",tctrl->nentry);
  59. fprintf(stdout,"table size in bytes: %d\n",mtrsiz(tctrl));
  60. fprintf(stdout,"# nodes: %3d  # links: %3d  #chars: %3d\n",
  61.     tctrl->nnodes,tctrl->nlinks,tctrl->nchars);
  62. xctrl=xalloc();
  63. if(!cdgx(tctrl,xctrl))
  64. {
  65. fprintf(stderr,"Error in cdgx module!\n");
  66. exit(1);
  67. }
  68. /* print the compressed tree nodes
  69. xprt(xctrl);
  70. */
  71. if((fp=fopen(fname,"r")) == NULL)
  72. {
  73. fprintf(stderr,"Unable to reopen input file!\n");
  74. exit(1);
  75. }
  76. nsym=0;
  77. while(gtstr(fp,str,MAXLEN) != EOF)
  78. {
  79. nsym++;
  80. if(sgx(xctrl,str) != nsym)
  81. fprintf(stdout,"Symbol #%3d not found in compressed tree!\n",nsym);
  82. }
  83. fclose(fp);
  84. fretre(tctrl);
  85. fprintf(stdout,"Size of compressed tree: %d\n",xctrl->size);
  86. }
  87.  
  88. /*--- GTSTR -----------------------------------------------------------
  89. Return a non-null line from the requested input device.  Any line longer
  90. than 'maxlength-1' is truncated.  The length of the line is normally 
  91. returned. EOF is returned on eof or error.
  92. -----------------------------------------------------------------------*/
  93.  
  94. gtstr(fp,str,maxlength)
  95. char *str;
  96. int maxlength;
  97. FILE *fp;
  98.  
  99. {
  100. int c=0,i=0;
  101. while(i==0 && c!=EOF)
  102. {
  103. while((c=getc(fp))!=EOF && c!='\n' && i<maxlength-1)
  104. {
  105. *str++=c;
  106. i++;
  107. }
  108. }
  109. *str='\0';
  110. return(c==EOF ? EOF : i);
  111. }
  112.